import openai
import numpy as np

# OPENAI_API_KEY = "填入专属的API key"

model = "text-embedding-ada-002"


def test1():
    text = "我喜欢你"
    openai.api_key = "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"

    emb_req = openai.Embedding.create(input=[text], model=model)

    emb = emb_req.data[0].embedding
    print(emb), print(len(emb)), print(type(emb))


def test2():
    a = [0.1, 0.2, 0.3]
    b = [0.2, 0.3, 0.4]
    cosine_ab = (0.1 * 0.2 + 0.2 * 0.3 + 0.3 * 0.4) / (
            np.sqrt(0.1 ** 2 + 0.2 ** 2 + 0.3 ** 2) * np.sqrt(0.2 ** 2 + 0.3 ** 2 + 0.4 ** 2))
    print(cosine_ab)

if __name__ == '__main__':
    test1()
